import java.util.Random;

/**
 * @author Macrydy
 * @date 2022/5/5
 *
 * Lambda用法
 * 条件：接口且只有一个抽象方法
 * 1.无参无返回
 * 2.有参无返回
 * 3.无参有返回
 * 4.有参有返回
 */
public class Test2 {
    public static void main(String[] args) {

        //1.无参无返回
        usePerson1(new Person1() {
            @Override
            public void work() {
                System.out.println("修电脑");
            }
        });

        //Lambda
        usePerson1(() -> System.out.println("lambda...修电脑"));

        //2.有参无返回
        usePerson2(new Person2() {
            @Override
            public void work(int a) {
                System.out.println(a+2);
            }
        });

        //Lambda
        usePerson2(a -> System.out.println(a));

        //3.无参有返回
        usePerson3(new Person3() {
            @Override
            public int work() {
                Random random = new Random();
                int i = random.nextInt(1000);
                return i;
            }
        });

        //Lambda
        usePerson3(() -> {
            Random random = new Random();
            int i = random.nextInt(1000);
            return i;
        });

        //4.有参有返回
        usePerson4(new Person4() {
            @Override
            public int work(int a, int b) {
                return a+b;
            }
        });

        //Lambda
        usePerson4((a, b) -> a+b);
    }

    public static void usePerson1(Person1 person){
        person.work();
    }
    public static void usePerson2(Person2 person){
        person.work(1);
    }
    public static void usePerson3(Person3 person){
        int work = person.work();
        System.out.println(work);
    }
    public static void usePerson4(Person4 person){
        int work = person.work(1, 2);
        System.out.println(work);
    }
}

interface Person1{
    void work();
}
interface Person2{
    void work(int a);
}
interface Person3{
    int work();
}
interface Person4{
    int work(int a,int b);
}